package com.globant;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidator {

	private Pattern pattern;
	private Matcher matcher;

	private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

	public EmailValidator() {
		pattern = Pattern.compile(EMAIL_PATTERN);
	}

	/**
	 * Validate if email is not registered and valid format
	 * 
	 * @param String
	 *            email for validation
	 * @return true if it is not registered and valid format, false if it is register or invalid format
	 */	
	public boolean validate(final String email) {
	if (isRegistered(email)==true){
		return false;
	}else{
		if (isValid(email)){
			return true;
		}
	}return true;
	

	}

	public boolean isRegistered(String email) {
		
		UserDAOImplementation userDao= new UserDAOImplementation();
		
		if(userDao.getUserByEmail(email)!=null){
		return true;
		}return false;
	}
	
	public boolean isValid(String email){
		
		 matcher = pattern.matcher(email);
		  return matcher.matches();		
	}
	
}
